<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../axios/axios.min.js"></script>
</head>

<body>
    <div>
        <h1>获取省份</h1>
        <button class='resolve'>成功</button>
        <button class="reject">失败</button>
        <p></p>
    </div>
    <script>
        document.querySelector('.resolve').addEventListener('click', function () {
            const p = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest()
                xhr.open('GET', 'http://hmajax.itheima.net/api/province')
                xhr.addEventListener('loadend', function () {
                    if (xhr.status >= 200 && xhr.status <= 299) {
                        resolve(JSON.parse(xhr.response).list)
                    }else{
                        reject(new Error(xhr.response))
                    }

                })
                xhr.send()
            })
            p.then(res => {
                document.querySelector('p').innerHTML = res

            }).catch(err=>{
                document.querySelector('p').innerHTML = err.message
            })
        })
        document.querySelector('.reject').addEventListener('click', function () {
            const p = new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest()
                xhr.open('GET', 'http://hmajax.itheima.net/api/provinc')
                xhr.addEventListener('loadend', function () {
                    if (xhr.status >= 200 && xhr.status <= 299) {
                        resolve(JSON.parse(xhr.response).list)
                    }else{
                        reject(new Error(xhr.response))
                    }

                })
                xhr.send()
            })
            p.then(res => {
                document.querySelector('p').innerHTML = res

            }).catch(err=>{
                console.log(err);
                
                document.querySelector('p').innerHTML = err.message
            })
        })
    </script>
</body>

</html>